<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@include file="common/head.jsp"%>
<script type="text/javascript">
    window.json=null;
    window.userCode=null;
    window.userRole=null;
</script>
<div class="layui-body" onload="init();">
    <div style="padding: 15px;">
        <div class="layui-tab-item layui-show">
            <form class="layui-form" action="">
                <div class="layui-form-item" style="margin-bottom: 0; margin-top: 10px;">
                    <label class="layui-form-label">用户ID:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="labNum" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入用户ID" class="layui-input" id="input_userCode">
                    </div>

                    <label class="layui-form-label">用户角色:</label>
                    <div class="layui-input-inline">
                        <select name="role" lay-verify="" lay-filter="select_role">
                            <option value=""></option>
                            <option value="0">学生</option>
                            <option value="1">教师</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <button type="button" class="layui-btn" id="upload">
                            <i class="layui-icon">&#xe67c;</i>批量导入
                        </button>
                    </div>
                </div>
            </form>
            <div>
                <%--account列表建立--%>
                <table id="account_table" lay-filter="account_table"></table>
            </div>
        </div>
    </div>
</div>
<%@include file="/jsp/root/common/foot.jsp" %>
<script type="text/javascript" id="operation">
    <a  type="button" title="重置密码" class="layui-btn layui-btn-radius layui-btn-normal" lay-event="reset">
        <i class="layui-icon">&#xe9aa;</i> 重置密码
    </a>
    <a id="del" title="删除" class="layui-btn layui-btn-radius layui-btn-danger" lay-event="delete">
        <i class="layui-icon">&#xe640;</i>删除
    </a>
</script>
<script type="text/javascript">
    /*初始化table*/
    function init() {
        let flag = sessionStorage.getItem("flag");
        if (flag == null || flag == undefined) {
            sessionStorage.setItem("flag", "yes");
            //调用显示弹窗方法
            showEject();
        }
        if (flag == "yes") {
            showEject();
        }
        userCode=null;
        userRole=null;
        render();
    }
    window.onload=init;
    //Excel文件上传
    layui.use('upload', function () {
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            , url: '${pageContext.request.contextPath}/jsp/upload.do' //上传接口
            ,accept: 'file'
            ,acceptMime: 'application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ,exts: 'xlsx|xls'//限制只能上传Excel文件
            , done: function (res) {
                layer.msg('已提交文件');
                render();
            }
            , error: function () {
                layer.msg('批量导入失败！');
            }
        });
    });
    /*excel文件上传小提示*/
    $('#upload').on('mouseover', function(){
        let that = this;
        layer.tips("<span style='font-size:14px'>"+'提交的excel需要遵循固定格式，如下：'+"<a href='${pageContext.request.contextPath}/jsp/root.do?method=downloadFile' target='_blank'>"+'[单击此处下载模板]'+"</a>"+"</span>", that,{ tips: [2, '#009688'],time:3000});
    });
    /*获取下拉菜单数据,用于模糊查询*/
    layui.use(['form','table'],function () {
        let form = layui.form;
        let role=null;
        let Code = $('#input_userCode').val();
        form.on('select(select_role)', function(data){
            role=data.value;
            /*模糊查询*/
            if ((Code==null || Code=="")&& role!=null){
                userCode=null;
                userRole=role;
                render();
            }
        });
    })
    /*获取输入框数据,用于精确查询*/
    $('#input_userCode').keydown(function (e) {
        let Code = $('#input_userCode').val();
        if (e.which == 13){
            /*精确查询*/
            if (Code!=null && Code!=""){
                userCode=Code;
                userRole=null;
                render();
            }
        }
    })
    /*查询数据*/
    function render(){
        layui.use('table', function() {
            let table = layui.table;
            table.render({
                elem: "#account_table",
                url: '${pageContext.request.contextPath}/jsp/root.do?method=getUserAccountList&userCode='+userCode+'&role='+userRole,
                size:'lg',
                skin:'row',
                page: true,
                limit: 9,
                limits:['1','5','9'],
                cols: [
                    [{
                        field: 'userCode',
                        title: '用户ID',
                        width: '25%',
                        unresize: 'true',
                        sort:true
                    },
                        {
                            field: 'userName',
                            title: '用户姓名',
                            width: '25%',
                            unresize: 'true'
                        },
                        {
                            field: 'role',
                            title: '用户角色',
                            width: '25%',
                            unresize: 'true'
                            ,templet:function (res) {
                                return res!=null && res.userRole==0?'学生':'教师';
                            }
                        },
                        {
                            field: 'method',
                            title: '操作',
                            toolbar:operation,
                            unresize: 'true'
                        }]
                ]
                ,parseData:function (res) {
                    let result;
                    if(this.page.curr){
                        result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
                    }
                    else{
                        result=res.data.slice(0,this.limit);
                    }
                    return{
                        "code":0
                        ,"data":result
                        ,"count": res.total
                    }
                }

            });

        });
    }
</script>
<script type="text/javascript">
    layui.use('table',function () {
        let table = layui.table;
        table.on('tool(account_table)',function (obj) {
            let data = obj.data;
            json = JSON.stringify(data);
            if(obj.event === 'reset'){
                reset();
            }else if(obj.event === 'delete'){
                deleteUser();
            }
        })
    })
</script>
<script>
    <%--function update(){--%>
    <%--    parent.layer.open({--%>
    <%--        type:2,--%>
    <%--        title:'用户信息更新',--%>
    <%--        shadeClose: true,--%>
    <%--        skin: 'yourclass',--%>
    <%--        content: "<%=path%>/jsp/root/eject/update.jsp",--%>
    <%--        area:['33%','60%'],--%>
    <%--        anim:5,--%>
    <%--    });--%>
    <%--}--%>
    <%-- 重置密码 --%>
    function reset(){
        layui.use(['layer'],function () {
            var layer = layui.layer;
            parent.layer.confirm('重置该用户密码为初始密码？',{icon: 3, title:'提示'},function (index) {
                $.ajax({
                    type:"POST"
                    ,url:"${pageContext.request.contextPath}/jsp/root.do?method=reset&userCode="+eval('('+json+')').userCode
                    ,dataType: "json"
                    ,success:function (data) {
                        if (data.ok === "yes"){
                            layer.msg('重置成功',{time:2000},function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        }else if (data.ok === 'no'){
                            layer.msg('重置失败');
                        }
                    }
                    ,error:function () {
                        layer.msg('连接异常，重置失败');
                    }
                })
                layer.close(index);
            })
        })
    }
    function deleteUser(){
        layui.use(['layer'],function(){
            var layer=layui.layer;
            parent.layer.confirm('确定删除？',{icon: 3, title:'提示'},function(index){
                $.ajax({
                    type:"POST"
                    ,dataType:"json"
                    ,url:"${pageContext.request.contextPath}/jsp/root.do?method=delete&userCode="+eval('('+json+')').userCode
                    ,success:function (data) {
                        if (data.ok === 'yes'){
                            layer.msg('删除成功',{time:2000},function () {
                                parent.render();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        }else if (data.ok === 'no'){
                            layer.msg('删除失败');
                        }
                    }
                    ,error:function () {
                        layer.msg('连接异常，删除失败');
                    }
                })
                layer.close(index);
            })
        })
    }
</script>
</body>
</html>